package models;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import util.DBAction;
import util.MyLogAction;

public class ProjectData {
	private int project_id;
	private String project_name;
	private String project_details;
	public int getProject_id() {
		return project_id;
	}
	public void setProject_id(int project_id) {
		this.project_id = project_id;
	}
	public String getProject_name() {
		return project_name;
	}
	public void setProject_name(String project_name) {
		this.project_name = project_name;
	}
	public String getProject_details() {
		return project_details;
	}
	public void setProject_details(String project_details) {
		this.project_details = project_details;
	}
	public int update() {
		String updateSQL = 
			    "update project "
			    + "set project_name = '" + project_name + 
				"', project_details = '" + project_details + 
				"where project_id = " + project_id;
		return DBAction.update(updateSQL);
	}

	public int insert() {
		String insertSQL = "insert into project "
				+ "(project_id, project_name, project_details) " + "values('" + project_id
				+ "', '" + project_name + "', '" + project_details + ");";
		return DBAction.update(insertSQL);
	}
	public static ArrayList<ProjectData> find(String selectionModifier) {
		ArrayList<ProjectData> projectList = new ArrayList<ProjectData>();
		ResultSet resultSet = null;
		String query = "select * "+
				"from project " + selectionModifier;
		Connection connection = DBAction.getConnection();
		resultSet = DBAction.readFromDB(query, connection);
		try {
			while (resultSet.next()) {
				ProjectData Project=new ProjectData();
				Project.project_id=resultSet.getInt("project_id");
				Project.project_name=resultSet.getString("project_name");
				Project.project_details=resultSet.getString("project_details");
				
				projectList.add(Project);
			}
		} catch (SQLException e) {
            MyLogAction.myCatch("ProjectData.java", 43, e);
			e.printStackTrace();
		}
		DBAction.close(resultSet);
		DBAction.close(connection);
		return projectList;
	}

	public static ProjectData findOne(String selectionModifier) {
		ResultSet resultSet = null;
		String query = "select * "+
				"from project " + selectionModifier;
		Connection connection = DBAction.getConnection();
		resultSet = DBAction.readFromDB(query, connection);
		try {
			if (resultSet.next()) {
				ProjectData Project=new ProjectData();
				Project.project_id=resultSet.getInt("project_id");
				Project.project_name=resultSet.getString("project_name");
				Project.project_details=resultSet.getString("project_details");
				
				DBAction.close(resultSet);
				DBAction.close(connection);
				return Project;
			}
		} catch (SQLException e) {
            MyLogAction.myCatch("ProjectData.java", 43, e);
			e.printStackTrace();
		}
		DBAction.close(resultSet);
		DBAction.close(connection);
		return null;
	}
	
}
